package com.hutianao.gqgl.organ.service.impl;

import com.alibaba.fastjson.JSON;
import com.hutianao.gqgl.mybatis.entity.PtOrganEntity;
import com.hutianao.gqgl.mybatis.entity.PtRoleEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.organ.entity.OrganBindEntity;
import com.hutianao.gqgl.organ.entity.OrganTree;
import com.hutianao.gqgl.organ.entity.SearchOrganEntity;
import com.hutianao.gqgl.organ.entity.SearchResultEntity;
import com.hutianao.gqgl.organ.respository.OrganDao;
import com.hutianao.gqgl.organ.service.OrganService;
import com.hutianao.gqgl.spring.util.DataUitl;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class OrganServiceImpl implements OrganService {
    @Autowired
    private OrganDao organDao;

    @Override
    public String addOrganInfo(PtOrganEntity ptOrganEntity) {
        ptOrganEntity.setOrganUuid(DataUitl.getUuid());
        ptOrganEntity.setModtime(new Date());
        PtOrganEntity parentOrgan = organDao.queryOrganListById(ptOrganEntity.getParentUuid());
        if(parentOrgan==null){
            ptOrganEntity.setParentUuid("-1");
        }
        organDao.addOrganInfo(ptOrganEntity);
        return "organ.create.success";
    }

    @Override
    public String getAllOrgan() {
        return JSON.toJSONString(organDao.getAllOrgan());

    }

    @Override
    public PadingRstType<SearchResultEntity> queryOrganInfoListByPage(SearchOrganEntity search,
                                                                      PaddingEntity padding) {
        padding.deal(PtRoleEntity.class);
        PadingRstType<SearchResultEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(padding.getPage());
        List<SearchResultEntity> list = null;
        Integer total = 0;
        if("1".equals(search.getOrganType())){
            // 岗位
            list = organDao.listDutyByName(search,padding);
            total = organDao.countDutyByName(search);
        }else if("2".equals(search.getOrganType())){
            list = organDao.listDepartmentByName(search,padding);
            total = organDao.countDepartmentByName(search);
        }else{
            list = organDao.listOrganByName(search,padding);
            total = organDao.countOrganByName(search);
        }

        padingRstType.setRawRecords(list);
        padingRstType.setTotal(total);
        padingRstType.putItems();

        return padingRstType;
    }

    @Override
    public String organBind(OrganBindEntity organBindEntity) {
        // check
        List<OrganTree> allOrgan = organDao.getAllOrgan();
        Optional<OrganTree> currentOrgan = allOrgan.stream()
                .filter(x -> x.getId().equals(organBindEntity.getOrganCurrentName())).findFirst();
        if(currentOrgan.isPresent()){
            Optional<OrganTree> parentOrgan = allOrgan.stream()
                    .filter(x -> x.getId().equals(organBindEntity.getOrganParentName())).findFirst();
            String pId= "-1";
            if(parentOrgan.isPresent()){
                pId = parentOrgan.get().getId();
            }
            organDao.organBind(currentOrgan.get().getId(),pId);
            return "organ.bind.success";
        }else{
            return "organ.bind.error";
        }

    }

    @Override
    public String organUpdate(OrganBindEntity organBindEntity) {
        List<OrganTree> allOrgan = organDao.getAllOrgan();
        Optional<OrganTree> currentOrgan = allOrgan.stream()
                .filter(x -> x.getName().equals(organBindEntity.getOrganParentName())).findFirst();
        if(currentOrgan.isPresent()){
            Optional<OrganTree> parentOrgan = allOrgan.stream()
                    .filter(x -> x.getName().equals(organBindEntity.getOrganCurrentName())).findFirst();
            if(parentOrgan.isPresent()){
                return "organ.update.error";
            }
            organDao.organUpdate(currentOrgan.get().getId(),organBindEntity.getOrganCurrentName());
            return "organ.update.success";
        }else{
            return "organ.update.error";
        }
    }

    @Override
    public String organUnBound(PtOrganEntity ptOrganEntity) {
        PtOrganEntity dbOrgan = organDao.queryOrganListById(ptOrganEntity.getOrganUuid());
        if(dbOrgan==null){
            return "organ.unbind.error";
        }else{
            organDao.organBind(dbOrgan.getOrganUuid(),"-1");
            return "organ.unbind.success";
        }
    }

    @Override
    public String deleteOrganById(String organUuidArray) {
        String[] organUuids = organUuidArray.split(",");
        // 组织结构存在以下情况时，不允许删除
        //1.有子组织结构
        //2.岗位
        //3.所属的国电部门
        int count = organDao.countReference(organUuids);
        if(count>0){
            return "organ.delete.error";
        }else{
            organDao.deleteOrganById(organUuids);
            return "organ.delete.success";
        }
    }

    @Override
    public String updateOrganInfo(PtOrganEntity ptOrganEntity) {
        organDao.updateOrganInfo(ptOrganEntity);
        return "organ.update.success";
    }

    @Override
    public PtOrganEntity queryOrganListById(String organUuid) {
        return organDao.queryOrganListById(organUuid);
    }

    @Override
    public String deleteOrganInfoByMenuId(String organUuid) {
        String[] organUuids={organUuid};
        // 组织结构存在以下情况时，不允许删除
        //1.有子组织结构
        //2.岗位
        //3.所属的国电部门
        int count = organDao.countReference(organUuids);
        if(count>0){
            return "organ.delete.error";
        }else{
            organDao.deleteOrganById(organUuids);
            return "organ.delete.success";
        }
    }
//alan
    @Override
    public void updateDictionaryInfo(PtOrganEntity ptOrganEntity) {

    }
//alan
    @Override
    public void deleteDictionaryInfoByDictionaryId(String dictId) {

    }
}